/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <map>
#include <string>
#include "WebErrorCodeList.h"
#include "napi/native_api.h"
#include "web/arkweb_net_error_list.h"
#include "web/arkweb_scheme_handler.h"
#include <hilog/log.h>
#include "WebTestUtils.h"


static void AddNetErrorByName(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> libErrorMap = {
        {"ARKWEB_ERR_UNEXPECTED_PROXY_AUTH", ARKWEB_ERR_UNEXPECTED_PROXY_AUTH},
        {"ARKWEB_ERR_EMPTY_RESPONSE", ARKWEB_ERR_EMPTY_RESPONSE},
        {"ARKWEB_ERR_RESPONSE_HEADERS_TOO_BIG", ARKWEB_ERR_RESPONSE_HEADERS_TOO_BIG},
        {"ARKWEB_ERR_PAC_SCRIPT_FAILED", ARKWEB_ERR_PAC_SCRIPT_FAILED},
        {"ARKWEB_ERR_REQUEST_RANGE_NOT_SATISFIABLE", ARKWEB_ERR_REQUEST_RANGE_NOT_SATISFIABLE},
        {"ARKWEB_ERR_IO_PENDING", ArkWeb_NetError::ARKWEB_ERR_IO_PENDING},
        {"ARKWEB_ERR_FAILED", ArkWeb_NetError::ARKWEB_ERR_FAILED},
        {"ARKWEB_ERR_ABORTED", ArkWeb_NetError::ARKWEB_ERR_ABORTED},
        {"ARKWEB_ERR_INVALID_HANDLE", ArkWeb_NetError::ARKWEB_ERR_INVALID_HANDLE},
        {"ARKWEB_ERR_TIMED_OUT", ArkWeb_NetError::ARKWEB_ERR_TIMED_OUT},
        {"ARKWEB_ERR_FILE_TOO_LARGE", ArkWeb_NetError::ARKWEB_ERR_FILE_TOO_LARGE},
        {"ARKWEB_ERR_UNEXPECTED", ArkWeb_NetError::ARKWEB_ERR_UNEXPECTED},
        {"ARKWEB_ERR_ACCESS_DENIED", ArkWeb_NetError::ARKWEB_ERR_ACCESS_DENIED},
        {"ARKWEB_ERR_NOT_IMPLEMENTED", ArkWeb_NetError::ARKWEB_ERR_NOT_IMPLEMENTED},
        {"ARKWEB_ERR_INSUFFICIENT_RESOURCES", ArkWeb_NetError::ARKWEB_ERR_INSUFFICIENT_RESOURCES},
        {"ARKWEB_ERR_OUT_OF_MEMORY", ArkWeb_NetError::ARKWEB_ERR_OUT_OF_MEMORY},
        {"ARKWEB_ERR_UPLOAD_FILE_CHANGED", ArkWeb_NetError::ARKWEB_ERR_UPLOAD_FILE_CHANGED},
        {"ARKWEB_ERR_SOCKET_NOT_CONNECTED", ArkWeb_NetError::ARKWEB_ERR_SOCKET_NOT_CONNECTED},
        {"ARKWEB_ERR_FILE_EXISTS", ArkWeb_NetError::ARKWEB_ERR_FILE_EXISTS},
        {"ARKWEB_ERR_FILE_PATH_TOO_LONG", ArkWeb_NetError::ARKWEB_ERR_FILE_PATH_TOO_LONG},
        {"ARKWEB_ERR_FILE_NO_SPACE", ArkWeb_NetError::ARKWEB_ERR_FILE_NO_SPACE},
        {"ARKWEB_ERR_FILE_VIRUS_INFECTED", ArkWeb_NetError::ARKWEB_ERR_FILE_VIRUS_INFECTED},
        {"ARKWEB_ERR_BLOCKED_BY_CLIENT", ArkWeb_NetError::ARKWEB_ERR_BLOCKED_BY_CLIENT},
        {"ARKWEB_ERR_NETWORK_CHANGED", ArkWeb_NetError::ARKWEB_ERR_NETWORK_CHANGED},
        {"ARKWEB_ERR_BLOCKED_BY_ADMINISTRATOR", ArkWeb_NetError::ARKWEB_ERR_BLOCKED_BY_ADMINISTRATOR},
        {"ARKWEB_ERR_SOCKET_CONNECTED", ArkWeb_NetError::ARKWEB_ERR_SOCKET_CONNECTED},
        {"ARKWEB_ERR_UPLOAD_STREAM_REWIND_NOT_SUPPORTED", ARKWEB_ERR_UPLOAD_STREAM_REWIND_NOT_SUPPORTED},
    };

    for (const auto& pair : libErrorMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddNetErrorConnectCode(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> libErrorMap = {
        {"ARKWEB_ERR_CONTEXT_SHUT_DOWN", ArkWeb_NetError::ARKWEB_ERR_CONTEXT_SHUT_DOWN},
        {"ARKWEB_ERR_BLOCKED_BY_RESPONSE", ArkWeb_NetError::ARKWEB_ERR_BLOCKED_BY_RESPONSE},
        {"ARKWEB_ERR_CLEARTEXT_NOT_PERMITTED", ArkWeb_NetError::ARKWEB_ERR_CLEARTEXT_NOT_PERMITTED},
        {"ARKWEB_ERR_BLOCKED_BY_CSP", ArkWeb_NetError::ARKWEB_ERR_BLOCKED_BY_CSP},
        {"ARKWEB_ERR_H2_OR_QUIC_REQUIRED", ArkWeb_NetError::ARKWEB_ERR_H2_OR_QUIC_REQUIRED},
        {"ARKWEB_ERR_BLOCKED_BY_ORB", ArkWeb_NetError::ARKWEB_ERR_BLOCKED_BY_ORB},
        {"ARKWEB_ERR_CONNECTION_CLOSED", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_CLOSED},
        {"ARKWEB_ERR_CONNECTION_RESET", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_RESET},
        {"ARKWEB_ERR_CONNECTION_REFUSED", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_REFUSED},
        {"ARKWEB_ERR_CONNECTION_ABORTED", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_ABORTED},
        {"ARKWEB_ERR_CONNECTION_FAILED", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_FAILED},
        {"ARKWEB_ERR_NAME_NOT_RESOLVED", ArkWeb_NetError::ARKWEB_ERR_NAME_NOT_RESOLVED},
        {"ARKWEB_ERR_INTERNET_DISCONNECTED", ArkWeb_NetError::ARKWEB_ERR_INTERNET_DISCONNECTED},
        {"ARKWEB_ERR_SSL_PROTOCOL_ERROR", ArkWeb_NetError::ARKWEB_ERR_SSL_PROTOCOL_ERROR},
        {"ARKWEB_ERR_ADDRESS_INVALID", ArkWeb_NetError::ARKWEB_ERR_ADDRESS_INVALID},
        {"ARKWEB_ERR_ADDRESS_UNREACHABLE", ArkWeb_NetError::ARKWEB_ERR_ADDRESS_UNREACHABLE},
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_NEEDED", ArkWeb_NetError::ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_NEEDED},
        {"ARKWEB_ERR_TUNNEL_CONNECTION_FAILED", ArkWeb_NetError::ARKWEB_ERR_TUNNEL_CONNECTION_FAILED},
        {"ARKWEB_ERR_NO_SSL_VERSIONS_ENABLED", ArkWeb_NetError::ARKWEB_ERR_NO_SSL_VERSIONS_ENABLED},
        {"ARKWEB_ERR_SSL_VERSION_OR_CIPHER_MISMATCH", ArkWeb_NetError::ARKWEB_ERR_SSL_VERSION_OR_CIPHER_MISMATCH},
        {"ARKWEB_ERR_SSL_RENEGOTIATION_REQUESTED", ArkWeb_NetError::ARKWEB_ERR_SSL_RENEGOTIATION_REQUESTED},
        {"ARKWEB_ERR_PROXY_AUTH_UNSUPPORTED", ArkWeb_NetError::ARKWEB_ERR_PROXY_AUTH_UNSUPPORTED},
        {"ARKWEB_ERR_BAD_SSL_CLIENT_AUTH_CERT", ArkWeb_NetError::ARKWEB_ERR_BAD_SSL_CLIENT_AUTH_CERT},
        {"ARKWEB_ERR_CONNECTION_TIMED_OUT", ArkWeb_NetError::ARKWEB_ERR_CONNECTION_TIMED_OUT},
        {"ARKWEB_ERR_HOST_RESOLVER_QUEUE_TOO_LARGE", ArkWeb_NetError::ARKWEB_ERR_HOST_RESOLVER_QUEUE_TOO_LARGE},
        {"ARKWEB_ERR_SOCKS_CONNECTION_FAILED", ArkWeb_NetError::ARKWEB_ERR_SOCKS_CONNECTION_FAILED},
        {"ARKWEB_ERR_SOCKS_CONNECTION_HOST_UNREACHABLE", ArkWeb_NetError::ARKWEB_ERR_SOCKS_CONNECTION_HOST_UNREACHABLE},
        {"ARKWEB_ERR_ALPN_NEGOTIATION_FAILED", ArkWeb_NetError::ARKWEB_ERR_ALPN_NEGOTIATION_FAILED},
    };

    for (const auto& pair : libErrorMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddWebErrorSSLCodeMap(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    const std::map<std::string, ArkWeb_NetError> libErrorMap = {
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT", ArkWeb_NetError::ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT},
        {"ARKWEB_ERR_ICANN_NAME_COLLISION", ArkWeb_NetError::ARKWEB_ERR_ICANN_NAME_COLLISION},
        {"ARKWEB_ERR_SSL_SERVER_CERT_BAD_FORMAT", ArkWeb_NetError::ARKWEB_ERR_SSL_SERVER_CERT_BAD_FORMAT},
        {"ARKWEB_ERR_CT_STH_PARSING_FAILED", ArkWeb_NetError::ARKWEB_ERR_CT_STH_PARSING_FAILED},
        {"ARKWEB_ERR_CT_STH_INCOMPLETE", ArkWeb_NetError::ARKWEB_ERR_CT_STH_INCOMPLETE},
        {"ARKWEB_ERR_UNABLE_TO_REUSE_CONNECTION_FOR_PROXY_AUTH",
         ArkWeb_NetError::ARKWEB_ERR_UNABLE_TO_REUSE_CONNECTION_FOR_PROXY_AUTH},
        {"ARKWEB_ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED",
         ArkWeb_NetError::ARKWEB_ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED},
        {"ARKWEB_ERR_SSL_OBSOLETE_CIPHER", ArkWeb_NetError::ARKWEB_ERR_SSL_OBSOLETE_CIPHER},
        {"ARKWEB_ERR_WS_UPGRADE", ArkWeb_NetError::ARKWEB_ERR_WS_UPGRADE},
        {"ARKWEB_ERR_READ_IF_READY_NOT_IMPLEMENTED", ArkWeb_NetError::ARKWEB_ERR_READ_IF_READY_NOT_IMPLEMENTED},
        {"ARKWEB_ERR_NO_BUFFER_SPACE", ArkWeb_NetError::ARKWEB_ERR_NO_BUFFER_SPACE},
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_NO_COMMON_ALGORITHMS",
         ArkWeb_NetError::ARKWEB_ERR_SSL_CLIENT_AUTH_NO_COMMON_ALGORITHMS},
        {"ARKWEB_ERR_EARLY_DATA_REJECTED", ArkWeb_NetError::ARKWEB_ERR_EARLY_DATA_REJECTED},
        {"ARKWEB_ERR_WRONG_VERSION_ON_EARLY_DATA", ArkWeb_NetError::ARKWEB_ERR_WRONG_VERSION_ON_EARLY_DATA},
        {"ARKWEB_ERR_TLS13_DOWNGRADE_DETECTED", ArkWeb_NetError::ARKWEB_ERR_TLS13_DOWNGRADE_DETECTED},
        {"ARKWEB_ERR_SSL_KEY_USAGE_INCOMPATIBLE", ArkWeb_NetError::ARKWEB_ERR_SSL_KEY_USAGE_INCOMPATIBLE},
    };

    for (const auto& pair : libErrorMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddNetErrorSSLCode(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> libErrorMap = {
        {"ARKWEB_ERR_SSL_NO_RENEGOTIATION", ArkWeb_NetError::ARKWEB_ERR_SSL_NO_RENEGOTIATION},
        {"ARKWEB_ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES", ArkWeb_NetError::ARKWEB_ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES},
        {"ARKWEB_ERR_SSL_DECOMPRESSION_FAILURE_ALERT", ArkWeb_NetError::ARKWEB_ERR_SSL_DECOMPRESSION_FAILURE_ALERT},
        {"ARKWEB_ERR_SSL_BAD_RECORD_MAC_ALERT", ArkWeb_NetError::ARKWEB_ERR_SSL_BAD_RECORD_MAC_ALERT},
        {"ARKWEB_ERR_PROXY_AUTH_REQUESTED", ArkWeb_NetError::ARKWEB_ERR_PROXY_AUTH_REQUESTED},
        {"ARKWEB_ERR_PROXY_CONNECTION_FAILED", ArkWeb_NetError::ARKWEB_ERR_PROXY_CONNECTION_FAILED},
        {"ARKWEB_ERR_MANDATORY_PROXY_CONFIGURATION_FAILED", ARKWEB_ERR_MANDATORY_PROXY_CONFIGURATION_FAILED},
        {"ARKWEB_ERR_PRECONNECT_MAX_SOCKET_LIMIT", ArkWeb_NetError::ARKWEB_ERR_PRECONNECT_MAX_SOCKET_LIMIT},
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED", ARKWEB_ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED},
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY", ARKWEB_ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY},
        {"ARKWEB_ERR_PROXY_CERTIFICATE_INVALID", ArkWeb_NetError::ARKWEB_ERR_PROXY_CERTIFICATE_INVALID},
        {"ARKWEB_ERR_NAME_RESOLUTION_FAILED", ArkWeb_NetError::ARKWEB_ERR_NAME_RESOLUTION_FAILED},
        {"ARKWEB_ERR_NETWORK_ACCESS_DENIED", ArkWeb_NetError::ARKWEB_ERR_NETWORK_ACCESS_DENIED},
        {"ARKWEB_ERR_TEMPORARILY_THROTTLED", ArkWeb_NetError::ARKWEB_ERR_TEMPORARILY_THROTTLED},
        {"ARKWEB_ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT", ARKWEB_ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT},
        {"ARKWEB_ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED", ArkWeb_NetError::ARKWEB_ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED},
        {"ARKWEB_ERR_MSG_TOO_BIG", ArkWeb_NetError::ARKWEB_ERR_MSG_TOO_BIG},
        {"ARKWEB_ERR_WS_PROTOCOL_ERROR", ArkWeb_NetError::ARKWEB_ERR_WS_PROTOCOL_ERROR},
        {"ARKWEB_ERR_ADDRESS_IN_USE", ArkWeb_NetError::ARKWEB_ERR_ADDRESS_IN_USE},
        {"ARKWEB_ERR_SSL_HANDSHAKE_NOT_COMPLETED", ArkWeb_NetError::ARKWEB_ERR_SSL_HANDSHAKE_NOT_COMPLETED},
        {"ARKWEB_ERR_SSL_BAD_PEER_PUBLIC_KEY", ArkWeb_NetError::ARKWEB_ERR_SSL_BAD_PEER_PUBLIC_KEY},
        {"ARKWEB_ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN", ArkWeb_NetError::ARKWEB_ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN},
        {"ARKWEB_ERR_CLIENT_AUTH_CERT_TYPE_UNSUPPORTED", ArkWeb_NetError::ARKWEB_ERR_CLIENT_AUTH_CERT_TYPE_UNSUPPORTED},
        {"ARKWEB_ERR_SSL_DECRYPT_ERROR_ALERT", ArkWeb_NetError::ARKWEB_ERR_SSL_DECRYPT_ERROR_ALERT},
        {"ARKWEB_ERR_WS_THROTTLE_QUEUE_TOO_LARGE", ArkWeb_NetError::ARKWEB_ERR_WS_THROTTLE_QUEUE_TOO_LARGE},
        {"ARKWEB_ERR_SSL_SERVER_CERT_CHANGED", ArkWeb_NetError::ARKWEB_ERR_SSL_SERVER_CERT_CHANGED},
        {"ARKWEB_ERR_SSL_UNRECOGNIZED_NAME_ALERT", ArkWeb_NetError::ARKWEB_ERR_SSL_UNRECOGNIZED_NAME_ALERT},
        {"ARKWEB_ERR_SOCKET_SET_RECEIVE_BUFFER_SIZE_ERROR", ARKWEB_ERR_SOCKET_SET_RECEIVE_BUFFER_SIZE_ERROR},
        {"ARKWEB_ERR_SOCKET_SET_SEND_BUFFER_SIZE_ERROR", ArkWeb_NetError::ARKWEB_ERR_SOCKET_SET_SEND_BUFFER_SIZE_ERROR},
        {"ARKWEB_ERR_SOCKET_RECEIVE_BUFFER_SIZE_UNCHANGEABLE", ARKWEB_ERR_SOCKET_RECEIVE_BUFFER_SIZE_UNCHANGEABLE},
        {"ARKWEB_ERR_SOCKET_SEND_BUFFER_SIZE_UNCHANGEABLE", ARKWEB_ERR_SOCKET_SEND_BUFFER_SIZE_UNCHANGEABLE},
    };
    
    for (const auto& pair : libErrorMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddWebNetErrorCERTAndECHCode(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> codeExMap = {
        {"ARKWEB_ERR_MALFORMED_IDENTITY", ARKWEB_ERR_MALFORMED_IDENTITY},
        {"ARKWEB_ERR_CONTENT_DECODING_FAILED", ARKWEB_ERR_CONTENT_DECODING_FAILED},
        {"ARKWEB_ERR_NETWORK_IO_SUSPENDED", ARKWEB_ERR_NETWORK_IO_SUSPENDED},
        {"ARKWEB_ERR_SYN_REPLY_NOT_RECEIVED", ARKWEB_ERR_SYN_REPLY_NOT_RECEIVED},
        {"ARKWEB_ERR_ENCODING_CONVERSION_FAILED", ARKWEB_ERR_ENCODING_CONVERSION_FAILED},
        {"ARKWEB_ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT", ARKWEB_ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT},
        {"ARKWEB_ERR_NO_SUPPORTED_PROXIES", ARKWEB_ERR_NO_SUPPORTED_PROXIES},
        {"ARKWEB_ERR_HTTP2_PROTOCOL_ERROR", ARKWEB_ERR_HTTP2_PROTOCOL_ERROR},
        {"ARKWEB_ERR_INVALID_AUTH_CREDENTIALS", ARKWEB_ERR_INVALID_AUTH_CREDENTIALS},
        {"ARKWEB_ERR_UNSUPPORTED_AUTH_SCHEME", ARKWEB_ERR_UNSUPPORTED_AUTH_SCHEME},
        {"ARKWEB_ERR_ENCODING_DETECTION_FAILED", ARKWEB_ERR_ENCODING_DETECTION_FAILED},
        {"ARKWEB_ERR_MISSING_AUTH_CREDENTIALS", ARKWEB_ERR_MISSING_AUTH_CREDENTIALS},
        {"ARKWEB_ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS", ARKWEB_ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS},
        {"ARKWEB_ERR_MISCONFIGURED_AUTH_ENVIRONMENT", ARKWEB_ERR_MISCONFIGURED_AUTH_ENVIRONMENT},
        {"ARKWEB_ERR_UNDOCUMENTED_SECURITY_LIBRARY_STATUS", ARKWEB_ERR_UNDOCUMENTED_SECURITY_LIBRARY_STATUS},
        {"ARKWEB_ERR_RESPONSE_BODY_TOO_BIG_TO_DRAIN", ARKWEB_ERR_RESPONSE_BODY_TOO_BIG_TO_DRAIN},
        {"ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH", ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH},
        {"ARKWEB_ERR_INCOMPLETE_HTTP2_HEADERS", ARKWEB_ERR_INCOMPLETE_HTTP2_HEADERS},
        {"ARKWEB_ERR_PAC_NOT_IN_DHCP", ARKWEB_ERR_PAC_NOT_IN_DHCP},
        {"ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION",
         ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION},
        {"ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION", ARKWEB_ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION},
        {"ARKWEB_ERR_HTTP2_SERVER_REFUSED_STREAM", ARKWEB_ERR_HTTP2_SERVER_REFUSED_STREAM},
        {"ARKWEB_ERR_HTTP2_PING_FAILED", ARKWEB_ERR_HTTP2_PING_FAILED},
        {"ARKWEB_ERR_CONTENT_LENGTH_MISMATCH", ARKWEB_ERR_CONTENT_LENGTH_MISMATCH},
        {"ARKWEB_ERR_INCOMPLETE_CHUNKED_ENCODING", ARKWEB_ERR_INCOMPLETE_CHUNKED_ENCODING},
    };

    for (const auto pair : codeExMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddNetErrorHTTPCode(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> codeExMap = {
        {"ARKWEB_ERR_QUIC_PROTOCOL_ERROR", ARKWEB_ERR_QUIC_PROTOCOL_ERROR},
        {"ARKWEB_ERR_RESPONSE_HEADERS_TRUNCATED", ARKWEB_ERR_RESPONSE_HEADERS_TRUNCATED},
        {"ARKWEB_ERR_QUIC_HANDSHAKE_FAILED", ARKWEB_ERR_QUIC_HANDSHAKE_FAILED},
        {"ARKWEB_ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY", ARKWEB_ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY},
        {"ARKWEB_ERR_HTTP2_FLOW_CONTROL_ERROR", ARKWEB_ERR_HTTP2_FLOW_CONTROL_ERROR},
        {"ARKWEB_ERR_HTTP2_FRAME_SIZE_ERROR", ARKWEB_ERR_HTTP2_FRAME_SIZE_ERROR},
        {"ARKWEB_ERR_HTTP2_COMPRESSION_ERROR", ARKWEB_ERR_HTTP2_COMPRESSION_ERROR},
        {"ARKWEB_ERR_PROXY_AUTH_REQUESTED_WITH_NO_CONNECTION", ARKWEB_ERR_PROXY_AUTH_REQUESTED_WITH_NO_CONNECTION},
        {"ARKWEB_ERR_HTTP_1_1_REQUIRED", ARKWEB_ERR_HTTP_1_1_REQUIRED},
        {"ARKWEB_ERR_PROXY_HTTP_1_1_REQUIRED", ARKWEB_ERR_PROXY_HTTP_1_1_REQUIRED},
        {"ARKWEB_ERR_PAC_SCRIPT_TERMINATED", ARKWEB_ERR_PAC_SCRIPT_TERMINATED},
        {"ARKWEB_ERR_INVALID_HTTP_RESPONSE", ARKWEB_ERR_INVALID_HTTP_RESPONSE},
        {"ARKWEB_ERR_CONTENT_DECODING_INIT_FAILED", ARKWEB_ERR_CONTENT_DECODING_INIT_FAILED},
        {"ARKWEB_ERR_HTTP2_RST_STREAM_NO_ERROR_RECEIVED", ARKWEB_ERR_HTTP2_RST_STREAM_NO_ERROR_RECEIVED},
        {"ARKWEB_ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE", ARKWEB_ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE},
        {"ARKWEB_ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER",
         ARKWEB_ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER},
        {"ARKWEB_ERR_TOO_MANY_RETRIES", ARKWEB_ERR_TOO_MANY_RETRIES},
        {"ARKWEB_ERR_HTTP2_STREAM_CLOSED", ARKWEB_ERR_HTTP2_STREAM_CLOSED},
        {"ARKWEB_ERR_HTTP2_CLIENT_REFUSED_STREAM", ARKWEB_ERR_HTTP2_CLIENT_REFUSED_STREAM},
        {"ARKWEB_ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH", ARKWEB_ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH},
        {"ARKWEB_ERR_HTTP_RESPONSE_CODE_FAILURE", ARKWEB_ERR_HTTP_RESPONSE_CODE_FAILURE},
        {"ARKWEB_ERR_QUIC_UNKNOWN_CERT_ROOT", ARKWEB_ERR_QUIC_UNKNOWN_CERT_ROOT},
        {"ARKWEB_ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED", ARKWEB_ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED},
        {"ARKWEB_ERR_TOO_MANY_ACCEPT_CH_RESTARTS", ARKWEB_ERR_TOO_MANY_ACCEPT_CH_RESTARTS},
        {"ARKWEB_ERR_INCONSISTENT_IP_ADDRESS_SPACE", ARKWEB_ERR_INCONSISTENT_IP_ADDRESS_SPACE},
        {"ARKWEB_ERR_CACHED_IP_ADDRESS_SPACE_BLOCKED_BY_LOCAL_NETWORK_ACCESS_POLICY",
         ARKWEB_ERR_CACHED_IP_ADDRESS_SPACE_BLOCKED_BY_LOCAL_NETWORK_ACCESS_POLICY},
        {"ARKWEB_ERR_CACHE_MISS", ARKWEB_ERR_CACHE_MISS},
        {"ARKWEB_ERR_CACHE_WRITE_FAILURE", ARKWEB_ERR_CACHE_WRITE_FAILURE},
        {"ARKWEB_ERR_CACHE_OPERATION_UNSUPPORTED", ARKWEB_ERR_CACHE_OPERATION_UNSUPPORTED},
        {"ARKWEB_ERR_CACHE_OPEN_FAILURE", ARKWEB_ERR_CACHE_OPEN_FAILURE},
        {"ARKWEB_ERR_CACHE_CREATE_FAILURE", ARKWEB_ERR_CACHE_CREATE_FAILURE},
        {"ARKWEB_ERR_CACHE_RACE", ARKWEB_ERR_CACHE_RACE},
        {"ARKWEB_ERR_CACHE_CHECKSUM_READ_FAILURE", ARKWEB_ERR_CACHE_CHECKSUM_READ_FAILURE},
        {"ARKWEB_ERR_CACHE_CHECKSUM_MISMATCH", ARKWEB_ERR_CACHE_CHECKSUM_MISMATCH},
        {"ARKWEB_ERR_CACHE_LOCK_TIMEOUT", ARKWEB_ERR_CACHE_LOCK_TIMEOUT},
        {"ARKWEB_ERR_CACHE_AUTH_FAILURE_AFTER_READ", ARKWEB_ERR_CACHE_AUTH_FAILURE_AFTER_READ},
        {"ARKWEB_ERR_CACHE_ENTRY_NOT_SUITABLE", ARKWEB_ERR_CACHE_ENTRY_NOT_SUITABLE},
        {"ARKWEB_ERR_CACHE_DOOM_FAILURE", ARKWEB_ERR_CACHE_DOOM_FAILURE},
        {"ARKWEB_ERR_CACHE_OPEN_OR_CREATE_FAILURE", ARKWEB_ERR_CACHE_OPEN_OR_CREATE_FAILURE},
    };

    for (const auto pair : codeExMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static void AddErrCodeFTPPandDNS(std::map<std::string, ArkWeb_NetError>& netErrorMap)
{
    std::map<std::string, ArkWeb_NetError> codeExMap = {
        {"ARKWEB_ERR_INSECURE_RESPONSE", ARKWEB_ERR_INSECURE_RESPONSE},
        {"ARKWEB_ERR_NO_PRIVATE_KEY_FOR_CERT", ARKWEB_ERR_NO_PRIVATE_KEY_FOR_CERT},
        {"ARKWEB_ERR_ADD_USER_CERT_FAILED", ARKWEB_ERR_ADD_USER_CERT_FAILED},
        {"ARKWEB_ERR_INVALID_SIGNED_EXCHANGE", ARKWEB_ERR_INVALID_SIGNED_EXCHANGE},
        {"ARKWEB_ERR_INVALID_WEB_BUNDLE", ARKWEB_ERR_INVALID_WEB_BUNDLE},
        {"ARKWEB_ERR_TRUST_TOKEN_OPERATION_FAILED", ARKWEB_ERR_TRUST_TOKEN_OPERATION_FAILED},
        {"ARKWEB_ERR_TRUST_TOKEN_OPERATION_SUCCESS_WITHOUT_SENDING_REQUEST",
         ARKWEB_ERR_TRUST_TOKEN_OPERATION_SUCCESS_WITHOUT_SENDING_REQUEST},
        {"ARKWEB_ERR_FTP_FAILED", ARKWEB_ERR_FTP_FAILED},
        {"ARKWEB_ERR_FTP_SERVICE_UNAVAILABLE", ARKWEB_ERR_FTP_SERVICE_UNAVAILABLE},
        {"ARKWEB_ERR_FTP_TRANSFER_ABORTED", ARKWEB_ERR_FTP_TRANSFER_ABORTED},
        {"ARKWEB_ERR_FTP_FILE_BUSY", ARKWEB_ERR_FTP_FILE_BUSY},
        {"ARKWEB_ERR_FTP_SYNTAX_ERROR", ARKWEB_ERR_FTP_SYNTAX_ERROR},
        {"ARKWEB_ERR_FTP_COMMAND_UNSUPPORTED", ARKWEB_ERR_FTP_COMMAND_UNSUPPORTED},
        {"ARKWEB_ERR_FTP_BAD_COMMAND_SEQUENCE", ARKWEB_ERR_FTP_BAD_COMMAND_SEQUENCE},
        {"ARKWEB_ERR_PKCS12_IMPORT_BAD_PASSWORD", ARKWEB_ERR_PKCS12_IMPORT_BAD_PASSWORD},
        {"ARKWEB_ERR_PKCS12_IMPORT_FAILED", ARKWEB_ERR_PKCS12_IMPORT_FAILED},
        {"ARKWEB_ERR_IMPORT_CA_CERT_NOT_CA", ARKWEB_ERR_IMPORT_CA_CERT_NOT_CA},
        {"ARKWEB_ERR_IMPORT_CERT_ALREADY_EXISTS", ARKWEB_ERR_IMPORT_CERT_ALREADY_EXISTS},
        {"ARKWEB_ERR_IMPORT_CA_CERT_FAILED", ARKWEB_ERR_IMPORT_CA_CERT_FAILED},
        {"ARKWEB_ERR_IMPORT_SERVER_CERT_FAILED", ARKWEB_ERR_IMPORT_SERVER_CERT_FAILED},
        {"ARKWEB_ERR_PKCS12_IMPORT_INVALID_MAC", ARKWEB_ERR_PKCS12_IMPORT_INVALID_MAC},
        {"ARKWEB_ERR_PKCS12_IMPORT_INVALID_FILE", ARKWEB_ERR_PKCS12_IMPORT_INVALID_FILE},
        {"ARKWEB_ERR_PKCS12_IMPORT_UNSUPPORTED", ARKWEB_ERR_PKCS12_IMPORT_UNSUPPORTED},
        {"ARKWEB_ERR_KEY_GENERATION_FAILED", ARKWEB_ERR_KEY_GENERATION_FAILED},
        {"ARKWEB_ERR_PRIVATE_KEY_EXPORT_FAILED", ARKWEB_ERR_PRIVATE_KEY_EXPORT_FAILED},
        {"ARKWEB_ERR_SELF_SIGNED_CERT_GENERATION_FAILED", ARKWEB_ERR_SELF_SIGNED_CERT_GENERATION_FAILED},
        {"ARKWEB_ERR_CERT_DATABASE_CHANGED", ARKWEB_ERR_CERT_DATABASE_CHANGED},
        {"ARKWEB_ERR_CERT_VERIFIER_CHANGED", ARKWEB_ERR_CERT_VERIFIER_CHANGED},
        {"ARKWEB_ERR_DNS_MALFORMED_RESPONSE", ARKWEB_ERR_DNS_MALFORMED_RESPONSE},
        {"ARKWEB_ERR_DNS_SERVER_REQUIRES_TCP", ARKWEB_ERR_DNS_SERVER_REQUIRES_TCP},
        {"ARKWEB_ERR_DNS_SERVER_FAILED", ARKWEB_ERR_DNS_SERVER_FAILED},
        {"ARKWEB_ERR_DNS_TIMED_OUT", ARKWEB_ERR_DNS_TIMED_OUT},
        {"ARKWEB_ERR_DNS_CACHE_MISS", ARKWEB_ERR_DNS_CACHE_MISS},
        {"ARKWEB_ERR_DNS_SEARCH_EMPTY", ARKWEB_ERR_DNS_SEARCH_EMPTY},
        {"ARKWEB_ERR_DNS_SORT_ERROR", ARKWEB_ERR_DNS_SORT_ERROR},
        {"ARKWEB_ERR_DNS_SECURE_RESOLVER_HOSTNAME_RESOLUTION_FAILED",
         ARKWEB_ERR_DNS_SECURE_RESOLVER_HOSTNAME_RESOLUTION_FAILED},
        {"ARKWEB_ERR_DNS_NAME_HTTPS_ONLY", ARKWEB_ERR_DNS_NAME_HTTPS_ONLY},
        {"ARKWEB_ERR_DNS_REQUEST_CANCELED", ARKWEB_ERR_DNS_REQUEST_CANCELED},
        {"ARKWEB_ERR_DNS_NO_MATCHING_SUPPORTED_ALPN", ARKWEB_ERR_DNS_NO_MATCHING_SUPPORTED_ALPN}};

    for (const auto pair : codeExMap) {
        netErrorMap[pair.first] = pair.second;
    }
}

static ArkWeb_NetError GetWebNetErrorCode(const char *name)
{
    std::string strName(name);
    std::map<std::string, ArkWeb_NetError> netErrorMap = {
        {"ARKWEB_ERR_INVALID_ECH_CONFIG_LIST", ARKWEB_ERR_INVALID_ECH_CONFIG_LIST},
        {"ARKWEB_ERR_ECH_NOT_NEGOTIATED", ARKWEB_ERR_ECH_NOT_NEGOTIATED},
        {"ARKWEB_ERR_ECH_FALLBACK_CERTIFICATE_INVALID", ARKWEB_ERR_ECH_FALLBACK_CERTIFICATE_INVALID},
        {"ARKWEB_ERR_CERT_COMMON_NAME_INVALID", ARKWEB_ERR_CERT_COMMON_NAME_INVALID},
        {"ARKWEB_ERR_CERT_DATE_INVALID", ARKWEB_ERR_CERT_DATE_INVALID},
        {"ARKWEB_ERR_CERT_AUTHORITY_INVALID", ARKWEB_ERR_CERT_AUTHORITY_INVALID},
        {"ARKWEB_ERR_CERT_CONTAINS_ERRORS", ARKWEB_ERR_CERT_CONTAINS_ERRORS},
        {"ARKWEB_ERR_CERT_NO_REVOCATION_MECHANISM", ARKWEB_ERR_CERT_NO_REVOCATION_MECHANISM},
        {"ARKWEB_ERR_CERT_UNABLE_TO_CHECK_REVOCATION", ARKWEB_ERR_CERT_UNABLE_TO_CHECK_REVOCATION},
        {"ARKWEB_ERR_CERT_REVOKED", ARKWEB_ERR_CERT_REVOKED},
        {"ARKWEB_ERR_CERT_INVALID", ARKWEB_ERR_CERT_INVALID},
        {"ARKWEB_ERR_CERT_WEAK_SIGNATURE_ALGORITHM", ARKWEB_ERR_CERT_WEAK_SIGNATURE_ALGORITHM},
        {"ARKWEB_ERR_CERT_NON_UNIQUE_NAME", ARKWEB_ERR_CERT_NON_UNIQUE_NAME},
        {"ARKWEB_ERR_CERT_WEAK_KEY", ARKWEB_ERR_CERT_WEAK_KEY},
        {"ARKWEB_ERR_CERT_NAME_CONSTRAINT_VIOLATION", ARKWEB_ERR_CERT_NAME_CONSTRAINT_VIOLATION},
        {"ARKWEB_ERR_CERT_VALIDITY_TOO_LONG", ARKWEB_ERR_CERT_VALIDITY_TOO_LONG},
        {"ARKWEB_ERR_CERTIFICATE_TRANSPARENCY_REQUIRED", ARKWEB_ERR_CERTIFICATE_TRANSPARENCY_REQUIRED},
        {"ARKWEB_ERR_CERT_SYMANTEC_LEGACY", ARKWEB_ERR_CERT_SYMANTEC_LEGACY},
        {"ARKWEB_ERR_CERT_KNOWN_INTERCEPTION_BLOCKED", ARKWEB_ERR_CERT_KNOWN_INTERCEPTION_BLOCKED},
        {"ARKWEB_ERR_SSL_OBSOLETE_VERSION_OR_CIPHER", ARKWEB_ERR_SSL_OBSOLETE_VERSION_OR_CIPHER},
        {"ARKWEB_ERR_CERT_END", ARKWEB_ERR_CERT_END},
        {"ARKWEB_ERR_INVALID_URL", ARKWEB_ERR_INVALID_URL},
        {"ARKWEB_ERR_DISALLOWED_URL_SCHEME", ARKWEB_ERR_DISALLOWED_URL_SCHEME},
        {"ARKWEB_ERR_UNKNOWN_URL_SCHEME", ARKWEB_ERR_UNKNOWN_URL_SCHEME},
        {"ARKWEB_ERR_INVALID_REDIRECT", ARKWEB_ERR_INVALID_REDIRECT},
        {"ARKWEB_ERR_TOO_MANY_REDIRECTS", ARKWEB_ERR_TOO_MANY_REDIRECTS},
        {"ARKWEB_ERR_UNSAFE_REDIRECT", ARKWEB_ERR_UNSAFE_REDIRECT},
        {"ARKWEB_ERR_UNSAFE_PORT", ARKWEB_ERR_UNSAFE_PORT},
        {"ARKWEB_ERR_INVALID_RESPONSE", ARKWEB_ERR_INVALID_RESPONSE},
        {"ARKWEB_ERR_INVALID_CHUNKED_ENCODING", ARKWEB_ERR_INVALID_CHUNKED_ENCODING},
        {"ARKWEB_ERR_METHOD_UNSUPPORTED", ARKWEB_ERR_METHOD_UNSUPPORTED},
    };
    
    AddNetErrorByName(netErrorMap);
    AddNetErrorConnectCode(netErrorMap);
    AddWebErrorSSLCodeMap(netErrorMap);
    AddNetErrorSSLCode(netErrorMap);
    AddWebNetErrorCERTAndECHCode(netErrorMap);
    AddNetErrorHTTPCode(netErrorMap);
    AddErrCodeFTPPandDNS(netErrorMap);
    auto it = netErrorMap.find(strName);
    if (it != netErrorMap.end()) {
        return it->second;
    }
    return ARKWEB_NET_OK;
}

napi_value WebArkWebNetError(napi_env env, napi_callback_info info)
{
    OH_LOG_DEBUG(LOG_APP, "WebArkWebSSLError start..");
    const char* url = "www.example.com";
    ArkWeb_Response* response = nullptr;
    OH_ArkWeb_CreateResponse(&response);
    int32_t ret = OH_ArkWebResponse_SetUrl(response, url);
    auto  netErrorName = WebTestUtils::GetArgsOneParameter(env, info);
    ArkWeb_NetError errorCode = GetWebNetErrorCode(netErrorName.get());
    ret = OH_ArkWebResponse_SetError(response, errorCode);
    ArkWeb_NetError errorCodeRet = OH_ArkWebResponse_GetError(response);
    
    napi_value result;
    if (errorCode == errorCodeRet) {
        napi_create_int32(env, 0, &result);
    } else {
        napi_create_int32(env, errorCodeRet, &result);
    }
    OH_ArkWeb_DestroyResponse(response);
    OH_LOG_DEBUG(LOG_APP, "WebArkWebSSLError end..");
    return result;
}

